package com.ziyun.erp.modules.capital.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ziyun.erp.modules.capital.dao.MerchantOrderAccountDao;
import com.ziyun.erp.modules.capital.entity.MerchantOrderAccountEntity;
import com.ziyun.erp.modules.capital.service.MerchantOrderAccountService;
import io.renren.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("merchantOrderAccountService")
public class MerchantOrderAccountServiceImpl extends ServiceImpl<MerchantOrderAccountDao, MerchantOrderAccountEntity>
		implements MerchantOrderAccountService {

	@Override
	public void queryPage(Page<MerchantOrderAccountEntity> page, Map<String, Object> params) {
		 // 是否冻结 0冻结 1 非冻结
		 Integer isFreeze = Integer.parseInt(params.get("isFreeze").toString());

		 //下单时间
		 List<String> orderCreateTimeList = new ArrayList<>();
		 Date beginCreateTime = null;
		 Date endCreateTime = null;
		 if (params.get("orderCreateTime") != null ){
			 orderCreateTimeList = (List<String>) params.get("orderCreateTime");
			 beginCreateTime =  DateUtils.stringToDate(orderCreateTimeList.get(0), DateUtils.DATE_PATTERN);
			 endCreateTime = DateUtils.stringToDate(orderCreateTimeList.get(1), DateUtils.DATE_PATTERN);
		 }

		 //收货时间
		List<String> orderReceiveTimeList = new ArrayList<>();
		Date beginReceiveTime = null;
		Date endReceiveTime = null;
		if (params.get("receivedTime") != null ){
			orderReceiveTimeList = (List<String>) params.get("receivedTime");
			beginReceiveTime =  DateUtils.stringToDate(orderReceiveTimeList.get(0), DateUtils.DATE_PATTERN);
			endReceiveTime = DateUtils.stringToDate(orderReceiveTimeList.get(1), DateUtils.DATE_PATTERN);
		}

		this.selectPage(page, new EntityWrapper<MerchantOrderAccountEntity>()
				.eq(params.get("orderAccountId")!=null,"order_account_id", params.get("orderAccountId"))
				.eq(params.get("orderId")!=null, "order_id", params.get("orderId"))
				.ge(isFreeze.intValue() == 0, "received_time", DateUtils.addDateDays(DateUtils.getDateStart(new Date()), -1))
				.lt(isFreeze.intValue() == 1, "received_time", DateUtils.addDateDays(DateUtils.getDateEnd(new Date()), -2))
				.between(orderCreateTimeList .size()>1, "order_create_time", beginCreateTime, endCreateTime)
				.between(orderReceiveTimeList .size()>1, "received_time", beginReceiveTime, endReceiveTime)
				);
	}

	/**
	 * 查询 生成商户未生产金额的订单
	 * @param page
	 * @return
	 */
	@Override
	public List<MerchantOrderAccountEntity> selectFinishSellerOrderNotSyn(Page<MerchantOrderAccountEntity> page) {
		return baseMapper.selectFinishSellerOrderNotSyn(page);
	}

	@Override
	public int updateByCashId(Integer applyCashId) {
		return baseMapper.updateByCashId(applyCashId);
	}

	@Override
	public void synSellerOrder() {
		baseMapper.synSellerOrder();
	}

	@Override
	public List<MerchantOrderAccountEntity> selectListByCashId(Integer cashId) {
		return baseMapper.selectListByCashId(cashId);
	}

}
